package org.jeecg.modules.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.config.CustomProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author guoyh
 * @ClassName AsyncService
 * @Description
 * @since 2020/9/12 13:02
 **/
@Service
@Slf4j
public class AsyncService {

    @Resource
    private CustomProperties customProperties;

    @Resource
    private RestTemplate restTemplate;

    @Async
    public String sendWxNotify(String body,String signature,String timestamp,String nonce){
        String url = String.format(customProperties.getWxNotifyUrl(),signature,timestamp,nonce);
        log.info("微信转发请求URL：{},body：{}",url,body);
        ResponseEntity<String> responseEntity = null;
        try{
            responseEntity = restTemplate.postForEntity(url,body,String.class);
            log.info("微信转发请求响应码：{},响应内容：{}",responseEntity.getStatusCodeValue(), StringUtils.isNotBlank(responseEntity.getBody()) ? responseEntity.getBody() : null);
        }catch (Exception e){
            e.printStackTrace();
        }

        String res = !Objects.isNull(responseEntity) && StringUtils.isNotBlank(responseEntity.getBody()) ? responseEntity.getBody() : "";
        log.info("微信转发请求返回的结果："+res);
        return res;

    }

}
